package com.jiemengshi.server.util;

import com.alibaba.fastjson.JSONObject;

public class ApiResult {
	public static final String SUCCESS;
	public static final String SYSTEM_ERROR;
	
	static {
		//初始化SUCCESS
		JSONObject ret = new JSONObject();
		ret.put("code", 0);
		ret.put("message", "success");
		ret.put("data", "{}");
		SUCCESS = ret.toJSONString();
		
		//初始化SYSTEM_ERROR
		ret = new JSONObject();
		ret.put("code", ErrorCodeMsg.SYSTEM_ERROR);
		ret.put("message", ErrorCodeMsg.getErrorMsg(ErrorCodeMsg.SYSTEM_ERROR));
		ret.put("data", "{}");
		SYSTEM_ERROR = ret.toJSONString();
	}

	public static String writeData(Object data){
		JSONObject ret = new JSONObject();
		
		ret.put("code", 0);
		ret.put("message", "success");
		
		ret.put("data", data);
		
		return ApiJson.toJson(ret);
	}

	public static String writeData(Object data, int total){
		JSONObject ret = new JSONObject();

		ret.put("code", 0);
		ret.put("message", "success");

		ret.put("data", data);
		ret.put("total", total);

		return ApiJson.toJson(ret);
	}

	public static String writeData(Object data, long total){
		JSONObject ret = new JSONObject();

		ret.put("code", 0);
		ret.put("message", "success");

		ret.put("data", data);
		ret.put("total", total);

		return ApiJson.toJson(ret);
	}
	
	public static String writeException(PrjException e){
		JSONObject ret = new JSONObject();
		
		ret.put("code", e.getErrCode());
		ret.put("message", e.getErrMsg());
		
		ret.put("data", "{}");
		
		return ApiJson.toJson(ret);
	}
}
